<?php

namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Projectlist extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        $this->assign('project_status', config('param.project_status'));
        return view();
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['delete_time', '=', 0];
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['title', 'like', '%' . $post['title'] . '%'];
        }
        if (empty($post['status']) || $post['status'] == '') {

        } else {
            $map[] = ['status', '=', $post['status']];
        }
        $data['count'] = Db::name('project')
            ->where($map)
            ->count();

        $data['data'] = Db::name('project')
            ->where($map)
            // ->fetchSql(true)
            ->order(['status'=>'asc','id'=>'desc'])->page(input("param.page"), input('param.limit'))->select();
        $project_status = config('param.project_status');
        foreach ($data['data'] as &$info) {
            $info['status_id']=$info['status'];
            $info['status'] = $project_status[$info['status']];
            $info['s_time'] == 0 ? $info['s_time'] = '--' : $info['s_time'] = date('Y-m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '--' : $info['e_time'] = date('Y-m-d', $info['e_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Project;
            if (!$validate->check($post)) {
                $this->error($validate->getError());
                exit;
            }
            Db::startTrans();
            try {
                $post['info_id'] = Db::name('infoview')->insertGetId(['info' => $this->request->param('info', '', null)]);
                unset($post['info']);
                $data['project_id'] = Db::name('project')->insertGetId($post);
                $data['uid'] = config('param.project_auto_uid');
                $data['user_type'] = 1;
                $data['c_time'] = time();
                DB::name('project_user')->insert($data);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $this->assign('project_status', config('param.project_status'));
            return view();
        }
    }


    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            Db::startTrans();
            try {
                $info = Db::name('project')->where(['id' => $post['id']])->useSoftDelete('delete_time', time())->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }


    public function editform()
    {

        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            if (count($post) == 2) {

                Db::startTrans();
                try {
                    Db::name('project')->where(['id' => $post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            } else {
                $validate = new \app\admin\validate\Project();
                if (!$validate->check($post)) {
                    $this->error($validate->getError());
                }
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                    unset($post['info']);
                    $post['ok_time']=time();//结算时间
                    Db::name('project')->where(['id' => $post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $this->assign('project_status', config('param.project_status'));
            $info = Db::name('project')
                ->where('id', input('param.id'))
                ->find();
            $info['s_time'] == 0 ? $info['s_time'] = '' : $info['s_time'] = date('Y-m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '' : $info['e_time'] = date('Y-m-d', $info['e_time']);
            $info['info'] = Db::name('infoview')
                ->where('id', $info['info_id'])
                ->value('info');
            $this->assign('info', $info);
            return view('add');
        }

    }


}